// Copyright (C) 2010  Joan Puigcerver Perez, <joapuipe@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <XMLUtils.hpp>

Year xml_get_year(const XMLCh * year)
{
  char * year_ch = XMLString::transcode( year );
  Year y = (Year) atoi(year_ch);
  XMLString::release(&year_ch);
  return y;
}

Media xml_get_media(const XMLCh * media)
{
  char * media_ch = XMLString::transcode( media );
  Media ret(media_ch);
  XMLString::release( &media_ch );
  return ret;
}

bool xml_get_suspended(const XMLCh * suspended)
{
  char * suspended_ch = XMLString::transcode( suspended );
  bool ret;

  if ( XMLString::equals(suspended_ch, "true") )
    ret = true;
  else
    ret = false;

  XMLString::release(&suspended_ch);
  return ret;
}

Genres xml_get_genre(const XMLCh * genre)
{
  char * genre_ch = XMLString::transcode( genre );
  Genres ret(genre_ch); 
  XMLString::release(&genre_ch);
  return ret;
}

Work xml_get_work(const XMLCh * work)
{
  char * work_ch = XMLString::transcode( work );
  Work ret(work_ch);
  XMLString::release(&work_ch);
  return ret;
}

std::string xml_string_escape(const std::string& str)
{
  std::string res;

  for(size_t i = 0; i < str.length(); ++i) {
    switch(str[i]) {
    case '"':
      res += "&quot;";
      break;
    case '\'':
      res += "&apos;";
      break;
    case '<':
      res += "&lt;";
      break;
    case '>':
      res += "&gt;";
      break;
    case '&':
      res += "&amp;";
      break;
    default:
      res += str[i]; 
    }
  }

  return res;
}
